<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Apply as ModelApply;

use app\common\library\Sms as Smslib;

/**
 * 申请管理接口
 */
class Apply extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    /**
     * 提交申请
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiSummary ()
     * @ApiBody ()
     * @ApiParams (name="type", type="str", required=true, sample="", description="申请类型:1=工厂,2=门店,3=公司")
     * @ApiParams (name="name", type="str", required=true, sample="", description="联系人")
     * @ApiParams (name="mobile", type="str", required=true, sample="", description="手机号码")
     * @ApiParams (name="captcha", type="str", required=true, sample="", description="验证码")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function addApply()
    {
        $param = request()->post();
        extract($param);
        $validate = validate('Apply'); //使用助手函数
        $result = $validate->check($param);
        if (!$result) {
            $this->error($validate->getError());
        }


        $ret = Smslib::check($mobile, $captcha, 'default');
        if (!$ret) {
            $this->error(__('验证码不正确'));
        }
        $param['user_id'] = $this->auth->id;
        $apply = new ModelApply($param);

        $res = $apply->allowField(true)->save();
        if ($res) {
            $this->success('提交成功');
        }
        $this->error('提交失败');
    }
}
